import app from '../utils/cloudbase';

// 确保登录状态的函数
async function ensureLogin() {
  const auth = app.auth();
  await auth.signInAnonymously();
}

//分页查询
export function page(params) {
  return new Promise((resolve, reject) => {
    ensureLogin().then(() => {
      app.callFunction({
        name: 'Order',
        data: {
          action: 'query',
          page: params.pageNum,
          pageSize: params.pageSize,
          orderId: params.orderId,
          state: params.state
        }
      }).then(res => {
        resolve({ data: res.result });
      }).catch(err => {
        reject(err);
      });
    }).catch(err => {
      reject(err);
    });
  });
}

//更新订单
export function update(params) {
  return new Promise((resolve, reject) => {
    ensureLogin().then(() => {
      app.callFunction({
        name: 'Order',
        data: {
          action: 'update',
          orderId: params.orderId,
          state: params.state,
          reason: params.reason
        }
      }).then(res => {
        resolve({ data: res.result });
      }).catch(err => {
        reject(err);
      });
    }).catch(err => {
      reject(err);
    });
  });
}

//查询订单详情
export function searchDetails(orderId) {
  return new Promise((resolve, reject) => {
    ensureLogin().then(() => {
      app.callFunction({
        name: 'Order',
        data: {
          action: 'query',
          orderId: orderId
        }
      }).then(res => {
        resolve({ data: res.result });
      }).catch(err => {
        reject(err);
      });
    }).catch(err => {
      reject(err);
    });
  });
}

// 获取日销售额
export function getDaySale(shopId) {
  return new Promise((resolve, reject) => {
    ensureLogin().then(() => {
      app.callFunction({
        name: 'Order',
        data: {
          action: 'getDaySale',
          shopId
        }
      }).then(res => {
        resolve({ data: res.result });
      }).catch(err => {
        reject(err);
      });
    }).catch(err => {
      reject(err);
    });
  });
}

// 获取月销售额
export function getMonthSale(shopId) {
  return new Promise((resolve, reject) => {
    ensureLogin().then(() => {
      app.callFunction({
        name: 'Order',
        data: {
          action: 'getMonthSale',
          shopId
        }
      }).then(res => {
        resolve({ data: res.result });
      }).catch(err => {
        reject(err);
      });
    }).catch(err => {
      reject(err);
    });
  });
}

// 处理退款
export function handleRefund(params) {
  return new Promise((resolve, reject) => {
    ensureLogin().then(() => {
      app.callFunction({
        name: 'Order',
        data: {
          action: 'handleRefund',
          orderId: params.orderId,
          result: params.result,
          comment: params.comment
        }
      }).then(res => {
        resolve({ data: res.result });
      }).catch(err => {
        reject(err);
      });
    }).catch(err => {
      reject(err);
    });
  });
}

// 获取评价列表
export function getEvaluations(orderId) {
  return new Promise((resolve, reject) => {
    ensureLogin().then(() => {
      app.callFunction({
        name: 'Order',
        data: {
          action: 'getEvaluations',
          orderId: orderId
        }
      }).then(res => {
        resolve({ data: res.result });
      }).catch(err => {
        reject(err);
      });
    }).catch(err => {
      reject(err);
    });
  });
}

// 更新评价
export function updateEvaluation(params) {
  return new Promise((resolve, reject) => {
    ensureLogin().then(() => {
      app.callFunction({
        name: 'Order',
        data: {
          action: 'updateEvaluation',
          orderId: params.orderId,
          evaluation: params.evaluation
        }
      }).then(res => {
        resolve({ data: res.result });
      }).catch(err => {
        reject(err);
      });
    }).catch(err => {
      reject(err);
    });
  });
}

// 获取所有订单（管理员用）
export function getAllOrders(params) {
  return new Promise((resolve, reject) => {
    ensureLogin().then(() => {
      app.callFunction({
        name: 'Order',
        data: {
          action: 'getAllOrders',
          page: params.pageNum,
          pageSize: params.pageSize,
          orderId: params.orderId,
          state: params.state
        }
      }).then(res => {
        resolve({ data: res.result });
      }).catch(err => {
        reject(err);
      });
    }).catch(err => {
      reject(err);
    });
  });
}

// 退款审核（管理员）
export function processRefund(params) {
  return new Promise((resolve, reject) => {
    ensureLogin().then(() => {
      app.callFunction({
        name: 'Order',
        data: {
          action: 'processRefund',
          ...params
        }
      }).then(res => {
        resolve({ data: res.result });
      }).catch(err => {
        reject(err);
      });
    }).catch(err => {
      reject(err);
    });
  });
}

// 强制修改订单状态（管理员）
export function forceUpdateStatus(params) {
  return new Promise((resolve, reject) => {
    ensureLogin().then(() => {
      app.callFunction({
        name: 'Order',
        data: {
          action: 'forceUpdateStatus',
          ...params
        }
      }).then(res => {
        resolve({ data: res.result });
      }).catch(err => {
        reject(err);
      });
    }).catch(err => {
      reject(err);
    });
  });
}

// 发货（管理员）
export function shipOrder(params) {
  return new Promise((resolve, reject) => {
    ensureLogin().then(() => {
      app.callFunction({
        name: 'Order',
        data: {
          action: 'shipOrder',
          ...params
        }
      }).then(res => {
        resolve({ data: res.result });
      }).catch(err => {
        reject(err);
      });
    }).catch(err => {
      reject(err);
    });
  });
}

// 获取区间订单统计
export function getOrderCountByDateRange(data) {
  console.log('调用 getOrderCountByDateRange，参数：', data);
  return new Promise((resolve, reject) => {
    ensureLogin().then(() => {
      app.callFunction({
        name: 'Order',
        data: {
          action: 'getOrderCountByDateRange',
          ...data
        }
      }).then(res => {
        console.log('getOrderCountByDateRange 云函数返回：', res);
        resolve({ data: res.result });
      }).catch(err => {
        console.error('getOrderCountByDateRange 云函数异常：', err);
        reject(err);
      });
    }).catch(err => {
      reject(err);
    });
  });
}

// 获取区间内每天已发货订单数
export function getDayShipped(data) {
  return new Promise((resolve, reject) => {
    ensureLogin().then(() => {
      app.callFunction({
        name: 'Order',
        data: {
          action: 'getDayShipped',
          ...data
        }
      }).then(res => {
        resolve({ data: res.result });
      }).catch(err => {
        reject(err);
      });
    }).catch(err => {
      reject(err);
    });
  });
}

// 获取区间评价率
export function getCommentRateByDateRange(data) {
  return new Promise((resolve, reject) => {
    ensureLogin().then(() => {
      app.callFunction({
        name: 'Order',
        data: {
          action: 'getCommentRateByDateRange',
          ...data
        }
      }).then(res => {
        resolve({ data: res.result });
      }).catch(err => {
        reject(err);
      });
    }).catch(err => {
      reject(err);
    });
  });
}

